package net.proef.sample.itcases;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.xml.XmlPage;

@RunWith(JUnit4.class)
public class BookSearchITCase {
    private final String PAGE_LOCATION = "http://localhost:8080/spring-sample/booksearch/books/";

    private WebClient webClient;
    private Logger logger = LoggerFactory.getLogger(BookSearchITCase.class);
    private XpathEngine engine = XMLUnit.newXpathEngine();

    public BookSearchITCase() {
	webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
    }

    @Test
    public void bookSearchShouldReturnXml() throws Exception {
	final XmlPage page = webClient.getPage(PAGE_LOCATION);

	assertThat(page, notNullValue());
	logger.trace("Got Content:\n{}", page.asText());

	NodeList list = this.engine.getMatchingNodes("//book", page.getXmlDocument());

	assertThat(list.getLength(), is(3));
    }
}
